package com.ymttest.database.sqlwapper.anlihui;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.exceptions.PersistenceException;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.anlihui.MemberRelation;

public class MemberRelationWrapper {
	
	SqlManager manager;

    public MemberRelationWrapper() {
        manager = new SqlManager("anlihui");
    }
    
    @SuppressWarnings("unchecked")
	public MemberRelation selectByPrimaryKey(Long id) {
    	
    	MemberRelation memberRelation = new MemberRelation();
    	List<MemberRelation> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.anlihui.MemberRelationMapper.selectByPrimaryKey",
                        id);
        manager.commit();
        
		if (result.size() > 0) {
			memberRelation = result.get(0);
		}
		
        return memberRelation;
    }
    
    @SuppressWarnings("unchecked")
	public MemberRelation selectByMemberId(Long memberId) {
    	
    	MemberRelation memberRelation = new MemberRelation();
    	List<MemberRelation> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.anlihui.MemberRelationMapper.selectByMemberId",
                        memberId);
        manager.commit();
        
		if (result.size() > 0) {
			
			memberRelation = result.get(0);
			
			return memberRelation;
		} else
			return null;
		
        
    }
    
    @SuppressWarnings("unchecked")
	public MemberRelation selectByMemberIdAndParentId(Long memberId, Long parentId) {
    	
    	HashMap<String, Long> map = new HashMap<>();
    	
    	map.put("member_id", memberId);
    	map.put("parent_id", parentId);
    	
    	MemberRelation memberRelation = new MemberRelation();
    	List<MemberRelation> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.anlihui.MemberRelationMapper.selectByMemberIdAndParentId",
                        map);
        manager.commit();
        
		if (result.size() > 0) {
			
			memberRelation = result.get(0);
			
			return memberRelation;
		} else
			return null;
    }
    
    public int insert(MemberRelation memberRelation) {
		
    	int count = 0;
    	
		try {
			count = manager
					.insert("com.ymttest.database.mapping.anlihui.MemberRelationMapper.insert",
							memberRelation);
			manager.commit();

		} catch (PersistenceException e) {
			// 如果插入了重复的记录，则删除原来的记录再插入一次
			e.printStackTrace();
		}
		
		return count;
    }
	
	public int updateMemberLevel(Long memberId, boolean upgradeable, int memberLevel) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("member_id", memberId);
		map.put("upgradeable", upgradeable);
		map.put("member_level", memberLevel);

		int count = manager
				.update("com.ymttest.database.mapping.anlihui.MemberRelationMapper.updateMemberLevelAndUpgradeableByMemberId",
						map);
		manager.commit();

		return count;
	}
	
	public int updateMemberOpenType(Long memberId, boolean upgradeable, int memberLevel, int openType) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("member_id", memberId);
		map.put("upgradeable", upgradeable);
		map.put("member_level", memberLevel);
		map.put("open_type", openType);

		int count = manager
				.update("com.ymttest.database.mapping.anlihui.MemberRelationMapper.updateMemberLevelAndOpenTypeByMemberId",
						map);
		manager.commit();

		return count;
	}
	
	public int deleteByMemberId(Long memberId) {
    	
    	int count = manager.delete("com.ymttest.database.mapping.anlihui.MemberRelationMapper.deleteByMemberId", memberId);
    	manager.commit();
    	
    	return count;
    }
    
    public static void main(String[] args) {
    	
    	MemberRelationWrapper test = new MemberRelationWrapper();
    	System.out.println(test.selectByMemberIdAndParentId(6050023980L, null).getBuyvip_order_id());
    }
}
